package com.example.dmcommom;

import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.annotation.Nulls;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * @author Zhaoph
 */
@Setter
@Getter
public class ResponseResult<T> implements Serializable {

	private int code = 0;

	private String msg;

	@JsonSetter(nulls = Nulls.AS_EMPTY)
	private T data;

	public ResponseResult(int code, String msg) {
		this.code = code;
		this.msg = msg;
	}

	public ResponseResult(int code, String msg, T data) {
		this.code = code;
		this.msg = msg;
		this.data = data;
	}

	public static <T> ResponseResult<T> success() {
		return new ResponseResult<>(200, "成功");
	}

	public static <T> ResponseResult<T> success(T data) {
		return new ResponseResult<>(200, "成功", data);
	}

	public static <T> ResponseResult<T> error(String msg) {
		return new ResponseResult<>(400, msg);
	}

	public static <T> ResponseResult<T> error(int code, String msg) {
		return new ResponseResult<>(code, msg);
	}

	public static <T> ResponseResult<T> error(int code, String msg, T data) {
		return new ResponseResult<>(code, msg, data);
	}

}
